<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('CPG_NUKE')) { exit; }

if (!is_user()) {
	URL::redirect(URL::index('Your_Account'), true);
}

$folders = array(
	'inbox'   => $LNG['Inbox'],
	'sentbox' => $LNG['Sentbox'],
	'outbox'  => $LNG['Outbox'],
	'savebox' => $LNG['Savebox']
);

$folder = $_POST->txt('folder') ?: $_GET->txt('folder');
$folder = isset($folders[$folder]) ? $folder : 'inbox';

$folder_title = $folders[$folder];
# Received a cancel request
if ($_POST->txt('cancel')) {
	URL::redirect(URL::index('&amp;folder='.$folder));
}

global $CPG_SESS;

define('PM_READ_MAIL', 0);
define('PM_NEW_MAIL', 1);
define('PM_SENT_MAIL', 2);
define('PM_SAVED_IN_MAIL', 3);
define('PM_SAVED_OUT_MAIL', 4);
define('PM_UNREAD_MAIL', 5);

$html_entities_match = array('#&(?!(\#[0-9]+;))#', '#<#', '#>#');
$html_entities_replace = array('&amp;', '&lt;', '&gt;');

$mode = htmlprepare($_POST->txt('mode') ?: $_GET->txt('mode'));


require_once('includes/nbbcode.php');

Dragonfly_Output_Js::add('includes/poodle/javascript/bbcode.js');
Dragonfly_Output_Css::add('bbcode');

Dragonfly_Output_Css::inline('
.pm_meter, .pm_boxes { width:100%; display:inline-block; text-align:center }
.pm_boxes > a, nav.pm * > * > * > * > a  { display:inline-block; margin:3px 5px}
.pm_boxes > a { width:70px }
');

# Copied function from phpBB/functions.php
function pm_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{
	global $lang;
	$total_pages = ceil($num_items/$per_page);
	if ($total_pages == 1) { return ''; }
	$on_page = floor($start_item / $per_page) + 1;
	$page_string = '';
	if ( $total_pages > 10 ) {
		$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
		for($i = 1; $i < $init_page_max + 1; $i++) {
			$page_string .= ( $i == $on_page ) ? '<b>'.$i.'</b>' : '<a href="'.URL::index($base_url.'&amp;start='.( ( $i - 1 ) * $per_page ) ).'">'.$i.'</a>';
			if ( $i <  $init_page_max ) { $page_string .= ', '; }
		}
		if ( $total_pages > 3 ) {
			if ( $on_page > 1  && $on_page < $total_pages ) {
				$page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';
				$init_page_min = ( $on_page > 4 ) ? $on_page : 5;
				$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
				for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++) {
					$page_string .= ($i == $on_page) ? '<b>'.$i.'</b>' : '<a href="'.URL::index($base_url.'&amp;start='.( ( $i - 1 ) * $per_page ) ).'">'.$i.'</a>';
					if ( $i <  $init_page_max + 1 ) { $page_string .= ', '; }
				}
				$page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
			} else {
				$page_string .= ' ... ';
			}
			for($i = $total_pages - 2; $i < $total_pages + 1; $i++) {
				$page_string .= ( $i == $on_page ) ? '<b>'.$i.'</b>'  : '<a href="'.URL::index($base_url.'&amp;start='.( ( $i - 1 ) * $per_page ) ).'">'.$i.'</a>';
				if( $i <  $total_pages ) { $page_string .= ", "; }
			}
		}
	} else {
		for($i = 1; $i < $total_pages + 1; $i++) {
			$page_string .= ( $i == $on_page ) ? '<b>'.$i.'</b>' : '<a href="'.URL::index($base_url."&amp;start=".( ( $i - 1 ) * $per_page ) ).'">'.$i.'</a>';
			if ( $i <  $total_pages ) { $page_string .= ', '; }
		}
	}
	if ( $add_prevnext_text ) {
		if ( $on_page > 1 ) {
			$page_string = ' <a href="'.URL::index($base_url."&amp;start=".( ( $on_page - 2 ) * $per_page ) ).'">'.$lang['Previous'].'</a>&nbsp;&nbsp;'.$page_string;
		}
		if ( $on_page < $total_pages ) {
			$page_string .= '&nbsp;&nbsp;<a href="'.URL::index($base_url."&amp;start=".( $on_page * $per_page ) ).'">'.$lang['Next'].'</a>';
		}
	}
	$page_string = $lang['Goto_page'].' '.$page_string;
	return $page_string;
}
